package com.lxmes.hr.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.HrGeneraRuleUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcRePunSummary;
import com.lxmes.hr.domain.NcWagesGzhs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcGsjsMapper;
import com.lxmes.hr.domain.NcGsjs;
import com.lxmes.hr.service.INcGsjsService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 个税计算Service业务层处理
 *
 * @author zhangzhiyu
 * @date 2023-01-16
 */
@Service
public class NcGsjsServiceImpl implements INcGsjsService
{
    @Autowired
    private NcGsjsMapper ncGsjsMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询个税计算
     *
     * @param id 个税计算主键
     * @return 个税计算
     */
    @Override
    public NcGsjs selectNcGsjsById(Long id)
    {
        return ncGsjsMapper.selectNcGsjsById(id);
    }

    /**
     * 查询个税计算列表
     *
     * @param ncGsjs 个税计算
     * @return 个税计算
     */
    @Override
    public List<NcGsjs> selectNcGsjsList(NcGsjs ncGsjs)
    {
        return ncGsjsMapper.selectNcGsjsList(ncGsjs);
    }

    /**
     * 新增个税计算
     *
     * @param ncGsjs 个税计算
     * @return 结果
     */
    @Override
    public int insertNcGsjs(NcGsjs ncGsjs)
    {
        ncGsjs.setCreateTime(DateUtils.getNowDate());
        return ncGsjsMapper.insertNcGsjs(ncGsjs);
    }

    /**
     * 修改个税计算
     *
     * @param ncGsjs 个税计算
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNcGsjs(NcGsjs ncGsjs)
    {
        ncGsjs.setYnssde(HrGeneraRuleUtils.ynssde(ncGsjs.getLjgz(),ncGsjs.getLjkcs(),ncGsjs.getLjsb(),ncGsjs.getZnjy(),ncGsjs.getSylr(),ncGsjs.getZfdklx(),ncGsjs.getZfzj(),ncGsjs.getJxjy()));
        ncGsjs.setRate(HrGeneraRuleUtils.slAndykl(ncGsjs.getYnssde()));
        ncGsjs.setSskcs(HrGeneraRuleUtils.sskcs(ncGsjs.getRate().doubleValue()));
        ncGsjs.setYnse(HrGeneraRuleUtils.ynse(ncGsjs.getYnssde(),ncGsjs.getRate().doubleValue(),ncGsjs.getSskcs()));
        ncGsjs.setSjse(ncGsjs.getYnse());
        ncGsjs.setBalance(HrGeneraRuleUtils.balance(ncGsjs.getYnse(),ncGsjs.getYjse()));

        ncGsjs.setUpdateTime(DateUtils.getNowDate());
        ncGsjs.setUpdateBy(SecurityUtils.getUsername());

        NcWagesGzhs ncWagesGzhs = new NcWagesGzhs();
        ncWagesGzhs.setJobNum(ncGsjs.getJobNo());
        ncWagesGzhs.setCurrentYm(ncGsjs.getNy());
        NcWagesGzhs gzhs = ncGsjsMapper.selectNcWagesGzhsByJob(ncWagesGzhs);
        gzhs.setGrsds(ncGsjs.getSjse());
        gzhs.setActualWagesTotal(gzhs.getActualWagesTotal().subtract(gzhs.getGrsds()));
        ncGsjsMapper.updateNcWagesGzhs(gzhs);
        return ncGsjsMapper.updateNcGsjs(ncGsjs);
    }

    /**
     * 批量删除个税计算
     *
     * @param ids 需要删除的个税计算主键
     * @return 结果
     */
    @Override
    public int deleteNcGsjsByIds(Long[] ids)
    {
        return ncGsjsMapper.deleteNcGsjsByIds(ids);
    }

    /**
     * 删除个税计算信息
     *
     * @param id 个税计算主键
     * @return 结果
     */
    @Override
    public int deleteNcGsjsById(Long id)
    {
        return ncGsjsMapper.deleteNcGsjsById(id);
    }

    @Override
    public String imporIteminfo(List<NcGsjs> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcGsjs org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcGsjs(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工工号 " + org.getJobNo() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getJobNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
